package ru.ifmo.tanya.core.users;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import ru.ifmo.tanya.db.DataBaseManager;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


public class UserManager {

    private ParameterizedRowMapper<User> mapper = new ParameterizedRowMapper<User>() {
        @Override
        public User mapRow(ResultSet resultSet, int i) throws SQLException {
            return new User(
                    resultSet.getInt("id"),
                    resultSet.getString("name"),
                    UserRole.valueOf(resultSet.getString("role").toUpperCase()),
                    resultSet.getString("extension")
            );
        }
    };

    public User getUserById(int id) {
        List<User> users = DataBaseManager.getJdbcTemplate().query("SELECT * FROM users WHERE id = ?", mapper, id);
        if (!users.isEmpty()) {
            return users.get(0);
        } else {
            return null;
        }
    }
}
